///后台任务进程
import 'dart:async';
import 'dart:isolate';
import 'dart:math';

import 'package:aio/aio.dart';
import 'package:android_alarm_manager_plus/android_alarm_manager_plus.dart';
import 'package:flutter/material.dart';
import 'package:app/src/http/model/todo_review_model.dart';

void _callback() async {
  //print("进来了");
  var share = await SharedPreferences.getInstance();
  //fixme 第一次初始化调用的时候，传递一下SharedPreferences
  DSharedPreferences prefs = getSharedPreferences(prefs: share);

  ///获取待审核数据。
  TodoReviewModel.getTdogetlist(null, false)?.then((value) {
    if (TodoReviewModel.newNotice == null) {
      if (value?.Data != null && value!.Data!.length > 0) {
        TodoReviewModel.newNotice = value.Data![0];
      }
    }
    // print(
    //     "======================================================================value：$value\t${TodoReviewModel.newNotice?.isNew()}");
    if ((TodoReviewModel.newNotice?.isNew() ?? false)) {
      TodoReviewModel.newNotice?.showNewNotification();
    }
    //TodoReviewModel.newNotice?.showNewNotification();
  });
}

appAndroidAlarmManagerPlusMain() async {
  // Be sure to add this line if initialize() call happens before runApp()
  try {
    WidgetsFlutterBinding.ensureInitialized();
    await AndroidAlarmManager.initialize();
    final int helloAlarmID =
        0; //fixme 任务id;   AndroidAlarmManager.cancel(id) 任务取消。
    delayed(1, () {
      //print("开始");
      //fixme 延迟几秒调用；防止AndroidAlarmManager未完全初始化完成。而导致 AndroidAlarmManager.periodic()调用方法无效。
      AndroidAlarmManager.periodic(
        const Duration(minutes: 3),
        //const Duration(seconds: 30),
        helloAlarmID,
        _callback,
      );
    });
  } catch (e) {
    print("后台进程插件异常：$e");
  }
}
